package value_object;


import java.util.ArrayList;
import java.util.List;

import value_object.data.Acquisition;
import value_object.data.Request;
import value_object.data.Strip;

public class InstanceVO extends VO {
	private List<Request> requestList;
	private List<Strip> stripList;
	
	public InstanceVO() {
		super();
		requestList = new ArrayList<Request>();
		stripList = new ArrayList<Strip>();
	}
	
	/**
	 * 
	 * @param data_set_number data set number
	 * @param grid_number grid number
	 * @param track_number track number
	 */
	public InstanceVO(int data_set_number, int grid_number, int track_number) {
		super(data_set_number, grid_number, track_number);
		requestList = new ArrayList<Request>();
	}	
	
	/**
	 * 
	 * @param request
	 * @return list of Strip
	 */
	public List<Strip> getStripListOf(Request request) {
		return request.getStripList();
	}
	
	@Override
	public String toString() {
		if (getRequestList().size() <= 0)
			return "";
		
		StringBuffer buffer = new StringBuffer();

		buffer.append(getData_set_number() + "(data-set-number)");
		buffer.append(" ");
		buffer.append(getGrid_number() + "(grid-number)");
		buffer.append(" ");
		buffer.append(getTrack_number() + "(track-number)");

		buffer.append("\n");

		buffer.append(this.getRequestList().size() + "(request-number)");
		buffer.append(" ");
		buffer.append(this.getAllStrips().size() + "(strip-number)");

		buffer.append("\n");

		buffer.append(0 + "(to-be-ignored)");
		buffer.append(" ");
		buffer.append(0 + "(to-be-ignored)");
		buffer.append(" ");
		buffer.append(0 + "(to-be-ignored)");

		buffer.append("\n\n");

		for (int i = 0; i < this.getRequestList().size(); i++) {
			buffer.append(this.getRequestList().get(i).toString());
			buffer.append("\n");
		}

		buffer.append("\n");

		for (int i = 0; i < this.getAllStrips().size(); i++) {
			buffer.append(this.getAllStrips().get(i).toString());
			buffer.append("\n\n");
		}

		return buffer.toString();
	}
	
	public String toSimpleString() {
		if (getRequestList().size() <= 0)
			return "";
		
		StringBuffer buffer = new StringBuffer();

		buffer.append(getData_set_number());
		buffer.append(" ");
		buffer.append(getGrid_number());
		buffer.append(" ");
		buffer.append(getTrack_number());

		buffer.append("\n");

		buffer.append(this.getRequestList().size());
		buffer.append(" ");
		buffer.append(this.getAllStrips().size());

		buffer.append("\n");

		buffer.append(0 + " " + 0 + " " + 0);

		buffer.append("\n\n");

		for (int i = 0; i < this.getRequestList().size(); i++) {
			buffer.append(this.getRequestList().get(i).toSimpleString());
			buffer.append("\n");
		}

		buffer.append("\n");

		for (int i = 0; i < this.getAllStrips().size(); i++) {
			buffer.append(this.getAllStrips().get(i).toSimpleString());
			buffer.append("\n\n");
		}

		return buffer.toString();
	}
	
	/*
	 * Getters and setters
	 */

	public List<Request> getRequestList() {
		return requestList;
	}

	public void setRequestList(List<Request> requestList) {
		this.requestList = requestList;
	}

	public void addRequest(Request request) {
		this.requestList.add(request);
	}
	
	public List<Strip> getStripList() {
		return stripList;
	}
	
	public void setStripList(List<Strip> stripList) {
		this.stripList = stripList;
	}
	
	public void addStrip(Strip strip) {
		this.stripList.add(strip);
	}
	
	/**
	 * Add a strip into a request
	 * 
	 * @param strip
	 */
	public void addStripToARequest(Strip strip) {
		for (int i = 0; i < getRequestList().size(); i++) {
			if (getRequestList().get(i).getRequest_index() == strip.getRequest_index()) {
				this.getRequestList().get(i).addAStrip(strip);
				return;
			}
		}
	}
	
	/**
	 * 
	 * @return all strips
	 */
	public List<Strip> getAllStrips() {
//		List<Strip> allStripsList = new ArrayList<Strip>();
//		
//		for (int i = 0; i < getRequestList().size(); i++) {
//			allStripsList.addAll(getRequestList().get(i).getStripList());
//		}
//		
//		return allStripsList;
		return stripList;
	}
	
	/**
	 * Get a list of strip from a solution file
	 * 
	 * @param solution
	 * @return
	 */
	public List<Strip> getStripListFrom(SolutionVO solution) {
		List<Strip> stripList = new ArrayList<Strip>();
		
		List<Acquisition> acqList = solution.getAcquisitionList();
			
		for (Acquisition acquisition : acqList) {
			Strip strip = getStripFrom(acquisition);
			if (strip != null) {
				stripList.add(strip);
			}
		}
		
		return stripList;
	}
	
	/**
	 * get a list of stereo request
	 * @return
	 */
	public List<Request> getStereoRequests() {
		final int request_stereo = 1;
		
		List<Request> requestList = new ArrayList<Request>();
		
		for (Request request : this.getRequestList()) {
			if (request.getRequest_stereo() == request_stereo)
				requestList.add(request);
		}
		return requestList;
	}
	
	/**
	 * get a Strip corresponding to acquisition from solution
	 * @param acq acquisition(strip_index, acq_direction) from solution
	 * @return
	 */
	public Strip getStripFrom(Acquisition acq) {
		for (Strip strip : getAllStrips()) {
			if (strip.getStrip_index() == acq.getStrip_index()) {
				strip.setStrip_direction(acq.getAcquisition_direction());
				return strip;
			}
		}
		return null;
	}
	
	/**
	 * Get a request by index
	 * 
	 * @param strip_index
	 * @param stripList
	 * @return
	 */
	public Request getRequestByIndex(int request_index) {
		for (int i = 0; i < getRequestList().size(); i++) {
			if (getRequestList().get(i).getRequest_index() == request_index)
				return getRequestList().get(i);
		}
		return null;
	}
}
